//
//  KhzlViewController.m
//  zhihuiguanjia
//
//  Created by apple on 16/4/3.
//  Copyright © 2016年 MyCompany. All rights reserved.
//

#import "KhzlViewController.h"
#import "MainViewController.h"
#import "MultilevelMenu.h"
#import "KhxqViewController.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@interface KhzlViewController ()<UIActionSheetDelegate>
{
    NSMutableArray *lis;
}

@property (nonatomic, strong) NSMutableArray *ypList;
@property (nonatomic, strong) NSMutableArray *zhuhuList;
@property (nonatomic, strong) UIButton *back;
@property (nonatomic, strong) NetworkManager *networkManager;
@property (nonatomic, strong) MBProgressHUD *HUD;

@end
@implementation KhzlViewController

- (void)viewDidLoad
{
    [self.navigationController setNavigationBarHidden:YES];
    self.ypList = [NSMutableArray array];
    self.zhuhuList = [NSMutableArray array];
    self.networkManager = [[NetworkManager alloc] init]; 
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD];
    self.HUD.delegate = self;
    [super viewDidLoad];
    [self getLouList];
}

- (IBAction)backToPre:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}


- (void)getLouList {
    NSString *xiaoqu_id = @"8";
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                           xiaoqu_id,@"xiaoqu_id", nil];
    [self.networkManager requestWithMethod:louList parameter:param delegate:self];
    [self.HUD show:YES];
    
}


- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFinishedWithResult:(id)data {
    [self.HUD hide:YES];
    if (methodId == louList) {
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSString * result = [data objectForKeyOrNil:@"result"];
            if([result isEqualToString:@"000"]){
                [self.ypList removeAllObjects];
                NSArray *results = [data objectForKeyOrNil:@"loulist"];
                if (![results isKindOfClass:[NSNull class]] && [results count] > 0) {
                    [self.ypList addObjectsFromArray:results];
                }
                
                /**
                 *  构建需要数据 2层或者3层数据 (ps 2层也当作3层来处理)
                 */
                NSInteger countMax= self.ypList.count;  
                if(countMax >0){
                    NSString *xiaoqu_id = @"8";
                    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                                       [self.ypList[0] objectForKeyOrNil:@"id"],@"lou_id",
                                       xiaoqu_id,@"xiaoqu_id",
                                       [self.ypList[0] objectForKeyOrNil:@"name"],@"lou_name",
                                       [self.ypList[0] objectForKeyOrNil:@"start_louceng"],@"start_louceng",
                                       [NSString stringWithFormat:@"%i", 0],@"lou_cixu",
                                       [self.ypList[0] objectForKeyOrNil:@"cengshu"],@"lou_cengshu",
                                        nil];
                    [self.networkManager requestWithMethod:zhuhuList parameter:param delegate:self];
                    [self.HUD show:YES];
                }
               
            } else if([result isEqualToString:@"001"]){
                NSString * resultinfo = [data objectForKeyOrNil:@"resultinfo"];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                                message:[resultinfo stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
                                                               delegate:nil
                                                      cancelButtonTitle:@"确定"
                                                      otherButtonTitles:nil];
                [alert show];
                
            }
        }
        
    } else if(methodId == zhuhuList){
        lis = [NSMutableArray arrayWithCapacity:0];
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSString * result = [data objectForKeyOrNil:@"result"];
            NSString * lou_cixu = [data objectForKeyOrNil:@"lou_cixu"];
            NSString * lou_name = [data objectForKeyOrNil:@"lou_name"];
            NSString * start_louceng = [data objectForKeyOrNil:@"start_louceng"];
            NSLog(@"@@@@@start_louceng%@",start_louceng);
            NSString * lou_cengshu = [data objectForKeyOrNil:@"lou_cengshu"];
            NSMutableArray * sub=[NSMutableArray arrayWithCapacity:0];
            if([result isEqualToString:@"000"]){
                NSArray *results = [data objectForKeyOrNil:@"zhuhulist"];
                if (![results isKindOfClass:[NSNull class]] && [results count] > 0) {
                    [self.zhuhuList  addObjectsFromArray:results];
                }
                NSInteger countMax= self.ypList.count;
                     rightMeun * meun=[[rightMeun alloc] init];
                     meun.meunName=[NSString stringWithFormat:@"%@",lou_name];
                     meun.ID = lou_cixu;
                for (int j = 1 ; j < [lou_cengshu intValue]/4+2; j++) {
                    rightMeun * meun1=[[rightMeun alloc] init];
                    meun1.meunName=[NSString stringWithFormat:@"%@   %d层～%d层",lou_name,(j-1)*4+[start_louceng intValue],(j-1)*4+[start_louceng intValue]+3>[lou_cengshu intValue]+2?[lou_cengshu intValue]+2:(j-1)*4+[start_louceng intValue]+3];
                    [sub addObject:meun1];
                    NSMutableArray *zList=[NSMutableArray arrayWithCapacity:0];
 
                    for(int z = 0;z < self.zhuhuList.count/[lou_cengshu intValue];z++){
                        for(int y = 1; y <= 4 ;y++){
                            if((y-1)*self.zhuhuList.count/[lou_cengshu intValue]+z+(4*(j-1)*self.zhuhuList.count/[lou_cengshu intValue])<self.zhuhuList.count){
                                rightMeun * meun2=[[rightMeun alloc] init];
                                
                                meun2.status =[self.zhuhuList[(y-1)*self.zhuhuList.count/[lou_cengshu intValue]+z+(4*(j-1)*self.zhuhuList.count/[lou_cengshu intValue])] objectForKeyOrNil:@"status"];
                                meun2.tradeStatus =[self.zhuhuList[(y-1)*self.zhuhuList.count/[lou_cengshu intValue]+z+(4*(j-1)*self.zhuhuList.count/[lou_cengshu intValue])] objectForKeyOrNil:@"tradeStatus"];
                                meun2.weituoStatus =[self.zhuhuList[(y-1)*self.zhuhuList.count/[lou_cengshu intValue]+z+(4*(j-1)*self.zhuhuList.count/[lou_cengshu intValue])] objectForKeyOrNil:@"weituoStatus"];
                                meun2.meunName=[NSString stringWithFormat:@"%@",[self.zhuhuList[(y-1)*self.zhuhuList.count/[lou_cengshu intValue]+z+(4*(j-1)*self.zhuhuList.count/[lou_cengshu intValue])] objectForKeyOrNil:@"nickName"]];
                                meun2.ID = [self.zhuhuList[(y-1)*self.zhuhuList.count/[lou_cengshu intValue]+z+(4*(j-1)*self.zhuhuList.count/[lou_cengshu intValue])] objectForKeyOrNil:@"id"];
                                 [zList addObject:meun2];
                            
                            } else {
                                rightMeun * meun=[[rightMeun alloc] init];
                                meun.status = @"-1";
                                [zList addObject:meun];
                            }
                        }
                    }
                        meun1.nextArray=zList;
                        meun.nextArray=sub;
                    
                }
                
                 [lis addObject:meun];
                
                for (int i=1; i<countMax; i++) {
                    NSString *xiaoqu_id = @"8";
                    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                                           [self.ypList[i] objectForKeyOrNil:@"id"],@"lou_id",
                                           [self.ypList[i] objectForKeyOrNil:@"name"],@"lou_name",
                                           [self.ypList[i] objectForKeyOrNil:@"start_louceng"],@"start_louceng",
                                           [NSString stringWithFormat:@"%i", i+1],@"lou_cixu",
                                           [self.ypList[i] objectForKeyOrNil:@"cengshu"],@"lou_cengshu",
                                           xiaoqu_id,@"xiaoqu_id", nil];
                    [self.networkManager requestWithMethod:zhuhuList1 parameter:param delegate:self];
                    [self.HUD show:YES];
                }
                
            }
        }
         [self.zhuhuList removeAllObjects];
    } else if(methodId == zhuhuList1){
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSString * result = [data objectForKeyOrNil:@"result"];
            NSString * lou_cixu = [data objectForKeyOrNil:@"lou_cixu"];
            NSString * lou_name = [data objectForKeyOrNil:@"lou_name"];
            NSString * lou_cengshu = [data objectForKeyOrNil:@"lou_cengshu"];
            NSString * start_louceng = [data objectForKeyOrNil:@"start_louceng"];
            NSLog(@"@@@@@start_louceng%@",start_louceng);
            NSMutableArray * sub=[NSMutableArray arrayWithCapacity:0];
            if([result isEqualToString:@"000"]){
                NSArray *results = [data objectForKeyOrNil:@"zhuhulist"];
                if (![results isKindOfClass:[NSNull class]] && [results count] > 0) {
                    [self.zhuhuList  addObjectsFromArray:results];
                }
                NSInteger countMax= self.ypList.count;
                
                rightMeun * meun=[[rightMeun alloc] init];
                meun.meunName=[NSString stringWithFormat:@"%@",lou_name];
                meun.ID = lou_cixu;
                
                for (int j = 1 ; j < [lou_cengshu intValue]/4+2; j++) {
                    rightMeun * meun1=[[rightMeun alloc] init];
                    meun1.meunName=[NSString stringWithFormat:@"%@   %d层～%d层", lou_name,(j-1)*4+[start_louceng intValue],(j-1)*4+[start_louceng intValue]+3>[lou_cengshu intValue]+2?[lou_cengshu intValue]+2:(j-1)*4+[start_louceng intValue]+3];
                    [sub addObject:meun1];
                    NSMutableArray *zList=[NSMutableArray arrayWithCapacity:0];
                    
//                    for ( int z = (self.zhuhuList.count/[lou_cengshu intValue]*4)*(j-1); z < (self.zhuhuList.count/[lou_cengshu intValue]*4)*j; z++) {
//                        rightMeun * meun2=[[rightMeun alloc] init];
//                        if(z<self.zhuhuList.count){
//                            meun2.status =[self.zhuhuList[z] objectForKeyOrNil:@"status"];
//                            meun2.meunName=[NSString stringWithFormat:@"%@",[self.zhuhuList[z] objectForKeyOrNil:@"nickName"]];
//                            meun2.ID = [self.zhuhuList[z] objectForKeyOrNil:@"id"];
//                            [zList addObject:meun2];
//                        }
//                    }
                    for(int z = 0;z < self.zhuhuList.count/[lou_cengshu intValue];z++){
                        for(int y = 1; y <= 4 ;y++){
                            if((y-1)*self.zhuhuList.count/[lou_cengshu intValue]+z+(4*(j-1)*self.zhuhuList.count/[lou_cengshu intValue])<self.zhuhuList.count){
                                rightMeun * meun2=[[rightMeun alloc] init];
                                
                                meun2.status =[self.zhuhuList[(y-1)*self.zhuhuList.count/[lou_cengshu intValue]+z+(4*(j-1)*self.zhuhuList.count/[lou_cengshu intValue])] objectForKeyOrNil:@"status"];
                                
                                meun2.tradeStatus =[self.zhuhuList[(y-1)*self.zhuhuList.count/[lou_cengshu intValue]+z+(4*(j-1)*self.zhuhuList.count/[lou_cengshu intValue])] objectForKeyOrNil:@"tradeStatus"];
                                meun2.weituoStatus =[self.zhuhuList[(y-1)*self.zhuhuList.count/[lou_cengshu intValue]+z+(4*(j-1)*self.zhuhuList.count/[lou_cengshu intValue])] objectForKeyOrNil:@"weituoStatus"];
                                meun2.meunName=[NSString stringWithFormat:@"%@",[self.zhuhuList[(y-1)*self.zhuhuList.count/[lou_cengshu intValue]+z+(4*(j-1)*self.zhuhuList.count/[lou_cengshu intValue])] objectForKeyOrNil:@"nickName"]];
                                meun2.ID = [self.zhuhuList[(y-1)*self.zhuhuList.count/[lou_cengshu intValue]+z+(4*(j-1)*self.zhuhuList.count/[lou_cengshu intValue])] objectForKeyOrNil:@"id"];
                                [zList addObject:meun2];
                                
                            } else {
                                rightMeun * meun=[[rightMeun alloc] init];
                                meun.status = @"-1";
                                [zList addObject:meun];
                            }
                        }
                    }
                    meun1.nextArray=zList;
                    meun.nextArray=sub;
                    
                }
                [lis addObject:meun];
                
                if(lis.count == countMax){
                    [lis sortUsingComparator:^NSComparisonResult(__strong id obj1,__strong id obj2){
                        rightMeun *menu1=(rightMeun *)obj1;
                        rightMeun *menu2=(rightMeun *)obj2;
                        
                        return [menu1.ID compare:menu2.ID];
                    }];
                    MultilevelMenu * view=[[MultilevelMenu alloc] initWithFrame:CGRectMake(0, 110, kScreenWidth, kScreenHeight-110) WithData:lis withSelectIndex:^(NSInteger left, NSInteger right,rightMeun* info) {
                        if(![[NSString stringWithFormat:@"%@",info.status] isEqualToString:@"-1"]){
                        CATransition* transition = [CATransition animation];
                        transition.type = kCATransitionPush;//可更改为其他方式
                        transition.subtype = kCATransitionFromLeft;//可更改为其他方式
                        KhxqViewController *problemVC = (KhxqViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"KhxqViewController"];
                        problemVC.zhuhuId  = info.ID;
                        [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
                        [self.navigationController pushViewController:problemVC animated:NO];
                        }
                    }];
                    view.isRecordLastScroll = YES;
                    [self.view addSubview:view];
                }
            }
        }
        [self.zhuhuList removeAllObjects];
        
    }
    
}

- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFailedWithError:(NSError *)error {
    NSDictionary *errorInfo = error.userInfo;
    NSString *msg = [errorInfo objectForKeyOrNil:@"errorMsg"];
    
    NSInteger code = error.code;
    if (code != 1) {
        if ([msg length] == 0) {
            msg = @"数据获取失败，请稍后重试";
        }
    }
    if ([msg length] > 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:msg
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
    }
}



@end
